<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" label-width="99px" v-show="showSearch" class="ele-form-search" @keyup.enter.native="reload"
             @submit.native.prevent>
      <el-row :gutter="15">
        <el-col :lg="8" :md="12">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
      <el-form-item label="质检任务号" prop="qualityInspectionNumber">
        <el-input
          v-model="queryParams.qualityInspectionNumber"
          placeholder="请输入质检任务号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
      <el-form-item label="产品编号" prop="materialNumber">
        <el-input
          v-model="queryParams.materialNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
      <el-form-item label="产品名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
<!--      <el-form-item label="检验数量" prop="inspectionQuantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.inspectionQuantity"-->
<!--          placeholder="请输入检验数量"-->
<!--          clearable-->
<!--          @input="aa"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
        </el-col>
        <el-col :lg="8" :md="12">
      <el-form-item label="检验结果" prop="testResult">
        <el-input
          v-model="queryParams.testResult"
          placeholder="请输入检验结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
      <el-form-item label="处理方式" prop="processingMode">
        <el-input
          v-model="queryParams.processingMode"
          placeholder="请输入处理方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
<!--      <el-form-item label="退货数量" prop="quantityReturned">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantityReturned"-->
<!--          placeholder="请输入退货数量"-->
<!--          clearable-->
<!--          @input="cc"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
        </el-col>
        <el-col :lg="8" :md="12">
<!--      <el-form-item label="检验完成时间" prop="inspectionCompletionTime">-->
          <!--        <el-date-picker clearable-->
          <!--          v-model="queryParams.inspectionCompletionTime"-->
          <!--          type="date"-->
          <!--          value-format="yyyy-MM-dd"-->
          <!--          placeholder="请选择检验完成时间">-->
          <!--        </el-date-picker>-->
          <!--      </el-form-item>-->
          <!--        </el-col>-->
          <!--        <el-col :lg="8" :md="12">-->
          <!--      <el-form-item label="校验员" prop="verifier">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.verifier"-->
          <!--          placeholder="请输入校验员"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
        </el-col>
        <el-col :lg="8" :md="12">
      <el-form-item label="来源单号" prop="sourceOrderNumber">
        <el-input
          v-model="queryParams.sourceOrderNumber"
          placeholder="请输入来源单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
<!--      <el-form-item label="创建人" prop="createUser">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.createUser"-->
          <!--          placeholder="请输入创建人"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
          <!--        </el-col>-->
          <!--        <el-col :lg="8" :md="12">-->
          <!--      <el-form-item label="最后更新人" prop="lastUpdater">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.lastUpdater"-->
          <!--          placeholder="请输入最后更新人"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
          <!--        </el-col>-->
          <!--        <el-col :lg="8" :md="12">-->
          <!--      <el-form-item label="最后更新时间" prop="lastUpdateTime">-->
          <!--        <el-date-picker clearable-->
          <!--          v-model="queryParams.lastUpdateTime"-->
          <!--          type="date"-->
          <!--          value-format="yyyy-MM-dd"-->
          <!--          placeholder="请选择最后更新时间">-->
          <!--        </el-date-picker>-->
          <!--      </el-form-item>-->
        </el-col>
        <el-col :lg="8" :md="12">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qualityManagement:incomingInspection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qualityManagement:incomingInspection:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qualityManagement:incomingInspection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['qualityManagement:incomingInspection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="incomingInspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="来料检记录ID" align="center" prop="inspectionId" />-->
      <el-table-column label="质检任务号" width="95"  align="center" prop="qualityInspectionNumber" />
      <el-table-column label="产品编号" width="95"  align="center" prop="materialNumber" />
      <el-table-column label="产品名称" width="95"  align="center" prop="materialName" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="检验数量" align="center" prop="inspectionQuantity" />
      <el-table-column label="检验结果" align="center" prop="testResult" />
      <el-table-column label="处理方式" align="center" prop="processingMode" />
      <el-table-column label="退货数量" align="center" prop="quantityReturned" />
      <el-table-column label="检验完成时间"  align="center" prop="inspectionCompletionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectionCompletionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="校验员" align="center" prop="verifier" />
      <el-table-column label="来源单号" align="center" prop="sourceOrderNumber" />
      <el-table-column label="创建人" align="center" prop="createUser" />
<!--      <el-table-column label="最后更新人" align="center" prop="lastUpdater" />-->
      <!--      <el-table-column label="最后更新时间" align="center" prop="lastUpdateTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作" width="125" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qualityManagement:incomingInspection:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qualityManagement:incomingInspection:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改来料检管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="15">
          <el-col :lg="12" :md="24">
        <el-form-item label="质检任务号" prop="qualityInspectionNumber">
          <el-input v-model="form.qualityInspectionNumber" placeholder="请输入质检任务号" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="产品编号" prop="materialNumber">
          <el-input v-model="form.materialNumber" placeholder="请输入产品编号" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="产品名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入产品名称" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="检验数量" prop="inspectionQuantity">
          <el-input @input="bb" v-model="form.inspectionQuantity" placeholder="请输入检验数量" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="检验结果" prop="testResult">
          <el-input v-model="form.testResult" placeholder="请输入检验结果" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="处理方式" prop="processingMode">
          <el-input v-model="form.processingMode" placeholder="请输入处理方式" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="退货数量" prop="quantityReturned">
          <el-input @input="dd" v-model="form.quantityReturned" placeholder="请输入退货数量" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="检验完成时间" prop="inspectionCompletionTime">
          <el-date-picker clearable
            v-model="form.inspectionCompletionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择检验完成时间">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="校验员" prop="verifier">
          <el-input v-model="form.verifier" placeholder="请输入校验员" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
        <el-form-item label="来源单号" prop="sourceOrderNumber">
          <el-input v-model="form.sourceOrderNumber" placeholder="请输入来源单号" />
        </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
<!--        <el-form-item label="创建人" prop="createUser">-->
            <!--          <el-input v-model="form.createUser" placeholder="请输入创建人" />-->
            <!--        </el-form-item>-->
            <!--          </el-col>-->
            <!--          <el-col :lg="12" :md="24">-->
            <!--        <el-form-item label="最后更新人" prop="lastUpdater">-->
            <!--          <el-input v-model="form.lastUpdater" placeholder="请输入最后更新人" />-->
            <!--        </el-form-item>-->
            <!--          </el-col>-->
            <!--          <el-col :lg="12" :md="24">-->
            <!--        <el-form-item label="最后更新时间" prop="lastUpdateTime">-->
            <!--          <el-date-picker clearable-->
            <!--            v-model="form.lastUpdateTime"-->
            <!--            type="date"-->
            <!--            value-format="yyyy-MM-dd"-->
            <!--            placeholder="请选择最后更新时间">-->
            <!--          </el-date-picker>-->
            <!--        </el-form-item>-->
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIncomingInspection, getIncomingInspection, delIncomingInspection, addIncomingInspection, updateIncomingInspection } from "@/api/qualityManagement/incomingInspection";
// 获取当前登录状态
import {getUserProfile} from "@/api/system/user";
import {getDate} from "@/utils/getTime";

export default {
  name: "IncomingInspection",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 来料检管理表格数据
      incomingInspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        qualityInspectionNumber: null,
        materialNumber: null,
        materialName: null,
        state: null,
        inspectionQuantity: null,
        testResult: null,
        processingMode: null,
        quantityReturned: null,
        inspectionCompletionTime: null,
        verifier: null,
        sourceOrderNumber: null,
        createUser: null,
        lastUpdater: null,
        lastUpdateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qualityInspectionNumber: [
          { required: true, message: "质检任务号不能为空", trigger: "blur" }
        ],
        materialName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        inspectionQuantity: [
          { required: true, message: "检验数量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUser()
  },
  methods: {

    aa(value){
      this.queryParams.inspectionQuantity=value.replace(/\D/g, '');
    },
    bb(value){
      this.form.inspectionQuantity=value.replace(/\D/g, '');
    },
    cc(value){
      this.queryParams. quantityReturned=value.replace(/\D/g, '');
    },
    dd(value){
      this.form. quantityReturned=value.replace(/\D/g, '');
    },
    /** 查询来料检管理列表 */
    getList() {
      this.loading = true;
      listIncomingInspection(this.queryParams).then(response => {
        this.incomingInspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inspectionId: null,
        qualityInspectionNumber: null,
        materialNumber: null,
        materialName: null,
        state: null,
        inspectionQuantity: null,
        testResult: null,
        processingMode: null,
        quantityReturned: null,
        inspectionCompletionTime: null,
        verifier: null,
        sourceOrderNumber: null,
        createUser: null,
        createTime: null,
        lastUpdater: null,
        lastUpdateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inspectionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加来料检管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inspectionId = row.inspectionId || this.ids
      getIncomingInspection(inspectionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改来料检管理";
      });
    },
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.inspectionId != null) {
            this.form.lastUpdater = this.user.userName
            this.form.lastUpdateTime = getDate()
            updateIncomingInspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 新增创建人
            this.form.creater = this.user.userName
            this.form.createUser = this.user.userName
            this.form.createTime = getDate()
            addIncomingInspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inspectionIds = row.inspectionId || this.ids;
      this.$modal.confirm('是否确认删除来料检管理编号为"' + inspectionIds + '"的数据项？').then(function() {
        return delIncomingInspection(inspectionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qualityManagement/incomingInspection/export', {
        ...this.queryParams
      }, `incomingInspection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
